{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "226fc196-329c-46c7-b160-ffedee46c695",
   "metadata": {},
   "source": [
    "# Graph RAG\n",
    "- 提取关键词\n",
    "- 查询图数据库检索上下文\n",
    "- 利用上下文生成答案"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16d9d331-6384-4c62-bbea-5f17e457292c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from model import RagLLM, QwenLLM\n",
    "from langchain import PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61bb5520-f4b5-4c90-9d6c-1d7d21d0af86",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "343161d8-65ec-4ae7-8181-265dd075df37",
   "metadata": {},
   "source": [
    "## 解析查询，提取关键词"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bdeede80-a0e2-4745-ab84-20369f792f56",
   "metadata": {},
   "outputs": [],
   "source": [
    "kg_llm = RagLLM()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1ba06863-502e-4342-9f51-95a57befeaa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"\"\"给定一些初始查询，提取最多 {max_keywords} 个相关关键词，考虑大小写、复数形式、常见表达等。\n",
    "用 '^' 符号分隔所有同义词/关键词：'关键词1^关键词2^...'\n",
    "\n",
    "注意，结果应为一行，用 '^' 符号分隔。\n",
    "----\n",
    "查询: {query_str}\n",
    "----\n",
    "关键词:\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e18d7742-8796-4af6-9ee7-48585720f8a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt_template = PromptTemplate(\n",
    "                input_variables=[\"query_str\", \"max_keywords\"],\n",
    "                template=prompt,\n",
    "            )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c0ed620-f3eb-461d-a522-3257fff06b06",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prompt = prompt_template.format(max_keywords='3',\n",
    "                             query_str=\"格力电器有什么事情发生\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7d95138c-5720-4138-b976-cbd36f470a4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"给定一些初始查询，提取最多 3 个相关关键词，考虑大小写、复数形式、常见表达等。\\n用 '^' 符号分隔所有同义词/关键词：'关键词1^关键词2^...'\\n\\n注意，结果应为一行，用 '^' 符号分隔。\\n----\\n查询: 格力电器有什么事情发生\\n----\\n关键词:\\n\""
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7c56eb5a-a295-4b6b-b5df-3432d8a41792",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'格力电器^事件^新闻'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kg_llm(final_prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6bae0976-d565-4019-8dda-d52d479d8c48",
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_query(query, max_keywords=3):\n",
    "    final_prompt = prompt_template.format(max_keywords='3',\n",
    "                                          query_str=query)\n",
    "    response = kg_llm(final_prompt)\n",
    "    keywords = response.split('\\n')[0].split('^')\n",
    "    return keywords"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3b8748ce-8d90-4251-86d1-fd614326bf73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['公司', '企业转型', '总结']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "parse_query(\"总结下哪些公司进行了企业转型？\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52a53a56-615a-4c99-818e-e0e5fd2a61fe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2b4fb89-2bf6-44c8-80e6-130ee139ed86",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "5b016b71-f086-4052-9431-b3df828f1963",
   "metadata": {},
   "source": [
    "## Graph RAG"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f091556b-7ac8-4bfc-8eb0-bb262d0e6d3f",
   "metadata": {},
   "source": [
    "### 通过关键词来查询节点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0cd0ebd3-40f1-4a79-b396-3b679ffca653",
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph, Node, Relationship"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a9cb313e-e1bd-442b-8e65-cbc785b37a1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = Graph(\"bolt://180.101.26.237:7687\", user='neo4j', password='neo4j@123',name='neo4j')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "17481d9b-213f-40fb-926e-03163ecf079f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_node(keyword, node_type):\n",
    "    query = f\"\"\"\n",
    "    MATCH (n:{node_type})\n",
    "    where n.name CONTAINS \"{keyword}\"\n",
    "    RETURN n.name as name\n",
    "    \"\"\"\n",
    "    results = graph.run(query)\n",
    "    print(query)\n",
    "    for record in results:\n",
    "        return record['name']\n",
    "    return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e04f9beb-9dca-4832-9849-ebd0d94e5526",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"比亚迪\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "None\n"
     ]
    }
   ],
   "source": [
    "res = get_node(\"比亚迪\", \"EventType\")\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c865f34a-9298-4051-9d6b-53fb7f8547e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"比亚迪\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "比亚迪股份有限公司\n"
     ]
    }
   ],
   "source": [
    "res = get_node(\"比亚迪\", \"Company\")\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "626e6f44-281c-487e-a9a1-41ead8ab3708",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"比亚迪\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "None\n"
     ]
    }
   ],
   "source": [
    "res = get_node(\"比亚迪\", \"Investor\")\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "730d10d3-01a7-4916-a072-322597558e2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1c2fa35b-87d6-4b70-9dfa-a1c177358715",
   "metadata": {},
   "source": [
    "### 通过节点来查询关联的关系和节点信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "25a0fa5b-6e12-4e77-8069-6984fa537cf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def gen_contexts(investor_condition, \n",
    "                 company_condition, \n",
    "                 even_type_condition,\n",
    "                 query_level=1,\n",
    "                 exclude_content=False):\n",
    "    if query_level == 1:\n",
    "        query = f\"\"\"\n",
    "        MATCH (i:Investor)-[:INVEST]->(c:Company)-[r:HAPPEN]->(e)\n",
    "        WHERE 1=1 {investor_condition} {company_condition} {even_type_condition}\n",
    "        RETURN i.name as investor,  c.name as company_name, e.name as even_type, r as relation\n",
    "        \"\"\"\n",
    "    else:\n",
    "        query = f\"\"\"\n",
    "        MATCH (c:Company)-[r:HAPPEN]->(e)\n",
    "        WHERE 1=1 {investor_condition} {company_condition} {even_type_condition}\n",
    "        RETURN c.name as company_name, e.name as even_type, r as relation\n",
    "        \"\"\"\n",
    "    print(query)\n",
    "    results = graph.run(query)\n",
    "    contexts = []\n",
    "    for record in results:\n",
    "        context = ''\n",
    "        record = dict(record)\n",
    "        if 'investor' in record:\n",
    "            context += f\"{record['investor']} 投资了 {record['company_name']} \\n\"\n",
    "        context = context + f\"{record['company_name']} 发生了 {record['even_type']} \\n 详细如下：\"\n",
    "        for key, value in dict(record['relation']).items():\n",
    "            if exclude_content:\n",
    "                if key in [\"title\", \"content\"]:\n",
    "                    continue\n",
    "            context = context + f\"\\n  {key}: {value}\"\n",
    "\n",
    "        contexts.append(context)\n",
    "    return contexts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f85f2541-cd36-45b2-9d4a-075cf4cb3827",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_even_detail(keyword, exclude_content=False):\n",
    "    investor = get_node(keyword, \"Investor\")\n",
    "    company = get_node(keyword, \"Company\")\n",
    "    even_type = get_node(keyword, \"EventType\")\n",
    "    \n",
    "    investor_condition = \"\"\n",
    "    company_condition = \"\"\n",
    "    even_type_condition = \"\"\n",
    "    if investor:\n",
    "        investor_condition = f' and i.name = \"{investor}\"'\n",
    "    if company:\n",
    "        company_condition = f' and c.name = \"{company}\"'\n",
    "    if even_type:\n",
    "        even_type_condition = f' and e.name = \"{even_type}\"'\n",
    "        \n",
    "    print(f\"investor={investor_condition} company={company_condition} even_type={even_type_condition}\")\n",
    "    if investor_condition or company_condition or even_type_condition:\n",
    "        contexts = gen_contexts(investor_condition, \n",
    "                                company_condition, \n",
    "                                even_type_condition,\n",
    "                                query_level=1,\n",
    "                                exclude_content=exclude_content)\n",
    "        if len(contexts) == 0:\n",
    "            contexts = gen_contexts(investor_condition, \n",
    "                                    company_condition, \n",
    "                                    even_type_condition,\n",
    "                                    query_level=2,\n",
    "                                    exclude_content=exclude_content)\n",
    "        return contexts\n",
    "    else:\n",
    "        return []\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c6e2cca8-8705-41e8-bcb4-de97efbf55a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= and c.name = \"瑞幸咖啡(北京)有限公司\" even_type=\n",
      "\n",
      "        MATCH (i:Investor)-[:INVEST]->(c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1   and c.name = \"瑞幸咖啡(北京)有限公司\" \n",
      "        RETURN i.name as investor,  c.name as company_name, e.name as even_type, r as relation\n",
      "        \n",
      "\n",
      "        MATCH (c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1   and c.name = \"瑞幸咖啡(北京)有限公司\" \n",
      "        RETURN c.name as company_name, e.name as even_type, r as relation\n",
      "        \n"
     ]
    }
   ],
   "source": [
    "contexts = get_even_detail(keyword=\"瑞幸咖啡\", exclude_content=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "8d56f23c-95a7-414c-b15d-26595d2bab29",
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "瑞幸咖啡(北京)有限公司 发生了 内部丑闻 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 4月2日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 停牌 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 4月7日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 股价异常波动 \n",
      " 详细如下：\n",
      "\n",
      "  数值: ['股价下跌了近89%']\n",
      "  时间: 4月7日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 造假欺诈 \n",
      " 详细如下：\n",
      "\n",
      "  数值: ['22亿元']\n",
      "  时间: 4月2日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 股权质押 \n",
      " 详细如下：\n",
      "\n",
      "  数值: ['4.88亿股', '49.13%', '1.45亿股', '30%']\n",
      "  时间: 本年1月\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 自查违规 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 7月1日晚间\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 复牌 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 5月20日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 资产被查封 \n",
      " 详细如下：\n",
      "\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 董监高—主动离职 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 4月23日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 退市 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 5月19日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 董监高—被迫离职 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 7月2日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 收到起诉 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 4月2日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 其他违规行为 \n",
      " 详细如下：\n",
      "\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 延期披露 \n",
      " 详细如下：\n",
      "\n",
      "  时间: 4月29日\n",
      "==================================================\n",
      "瑞幸咖啡(北京)有限公司 发生了 产品服务负面消息 \n",
      " 详细如下：\n",
      "\n",
      "==================================================\n"
     ]
    }
   ],
   "source": [
    "for c in contexts:\n",
    "    print(c)\n",
    "    print(\"=\"*50)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c1ecd5c-2c51-4d1e-95a0-e318a9b0f007",
   "metadata": {},
   "source": [
    "## graph rag pipline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c2d27349-cf4c-44c6-9b42-ff6fdea1d051",
   "metadata": {},
   "outputs": [],
   "source": [
    "rag_llm = RagLLM()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "03fe1772-21d9-4eac-ab74-b3135e082c50",
   "metadata": {},
   "outputs": [],
   "source": [
    "finance_template = \"\"\"\n",
    "你是金融知识助手，熟悉各种金融事件，需要根据提供的上下文信息context来回答员工的提问。\\\n",
    "请直接回答问题，如果上下文信息context没有和问题相关的信息，请直接先回答不知道,要求用中文输出 \\\n",
    "问题：{question} \n",
    "上下文信息：\n",
    "\"{context}\"\n",
    "回答：\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "15a6b4a8-4606-418c-987b-4096855df131",
   "metadata": {},
   "outputs": [],
   "source": [
    "def graph_rag_pipline(query, exclude_content=True, stream=True, temperature=0.1):\n",
    "    keywords = parse_query(query, max_keywords=3)\n",
    "    contexts = []\n",
    "    ignore_words = ['公司', '分析', '投资']\n",
    "    for keyword in keywords:\n",
    "        if keyword in ignore_words:\n",
    "            continue\n",
    "        contexts.extend(get_even_detail(keyword=keyword, exclude_content=exclude_content))\n",
    "    \n",
    "    prompt = PromptTemplate(\n",
    "                        input_variables=[\"question\", \"context\"],\n",
    "                        template=finance_template,)\n",
    "    llm_prompt = prompt.format(question=query, context=\"\\n========================\\n\".join(contexts))\n",
    "    print(llm_prompt)\n",
    "    \n",
    "    if stream:\n",
    "        response = rag_llm(llm_prompt, stream=True)\n",
    "        print(f\"response: \")\n",
    "        for chunk in response:\n",
    "            print(chunk.choices[0].text, end='', flush=True)\n",
    "        return \"\"\n",
    "    else:\n",
    "        response = rag_llm(llm_prompt, stream=False, temperature=temperature)\n",
    "        return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "4f0d80ea-0ee6-4c5d-afdd-183fbfdac398",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"企业转型\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"企业转型\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"企业转型\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= even_type= and e.name = \"企业转型\"\n",
      "\n",
      "        MATCH (i:Investor)-[:INVEST]->(c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1    and e.name = \"企业转型\"\n",
      "        RETURN i.name as investor,  c.name as company_name, e.name as even_type, r as relation\n",
      "        \n",
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"总结\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"总结\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"总结\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= even_type=\n",
      "\n",
      "你是金融知识助手，熟悉各种金融事件，需要根据提供的上下文信息context来回答员工的提问。请直接回答问题，如果上下文信息context没有和问题相关的信息，请直接先回答不知道,要求用中文输出 问题：总结下哪些公司进行了企业转型？ \n",
      "上下文信息：\n",
      "\"盈科资本 投资了 东易日盛家居装饰集团股份有限公司 \n",
      "东易日盛家居装饰集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年一季度\n",
      "============\n",
      "鑫盛汇股权投资 投资了 东易日盛家居装饰集团股份有限公司 \n",
      "东易日盛家居装饰集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年一季度\n",
      "============\n",
      "湖北省国有资本运营有限公司 投资了 东易日盛家居装饰集团股份有限公司 \n",
      "东易日盛家居装饰集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年一季度\n",
      "============\n",
      "峰尚资本 投资了 东易日盛家居装饰集团股份有限公司 \n",
      "东易日盛家居装饰集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年一季度\n",
      "============\n",
      "智动无限 投资了 东易日盛家居装饰集团股份有限公司 \n",
      "东易日盛家居装饰集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年一季度\n",
      "============\n",
      "泥巴创客空间 投资了 新希望六和股份有限公司 \n",
      "新希望六和股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年第一季度\n",
      "============\n",
      "固高科技 投资了 新希望六和股份有限公司 \n",
      "新希望六和股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年第一季度\n",
      "============\n",
      "深圳佳禾股权投资 投资了 新希望六和股份有限公司 \n",
      "新希望六和股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2020年第一季度\n",
      "============\n",
      "景德镇安鹏汽车产业创业投资合伙企业(有限合伙) 投资了 光明乳业股份有限公司 \n",
      "光明乳业股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2019年\n",
      "============\n",
      "工银瑞投-鑫和新三板3号资产管理计划 投资了 光明乳业股份有限公司 \n",
      "光明乳业股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2019年\n",
      "============\n",
      "济南尧尚贸易有限公司 投资了 光明乳业股份有限公司 \n",
      "光明乳业股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2019年\n",
      "============\n",
      "创丰资本 投资了 光明乳业股份有限公司 \n",
      "光明乳业股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 2019年\n",
      "============\n",
      "九合创投 投资了 大禹节水集团股份有限公司 \n",
      "大禹节水集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 从2017年\n",
      "============\n",
      "臻云创投(臻云智能) 投资了 大禹节水集团股份有限公司 \n",
      "大禹节水集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 从2017年\n",
      "============\n",
      "中瑞创投 投资了 大禹节水集团股份有限公司 \n",
      "大禹节水集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 从2017年\n",
      "============\n",
      "南京烽火星空通信发展有限公司 投资了 比亚迪股份有限公司 \n",
      "比亚迪股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "============\n",
      "美高森美通信科技 投资了 比亚迪股份有限公司 \n",
      "比亚迪股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "============\n",
      "兰石投资 投资了 比亚迪股份有限公司 \n",
      "比亚迪股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "============\n",
      "雍创资本 投资了 比亚迪股份有限公司 \n",
      "比亚迪股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "============\n",
      "策源资本 投资了 深圳市怡亚通供应链股份有限公司 \n",
      "深圳市怡亚通供应链股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "============\n",
      "南京理索纳投资管理有限公司 投资了 北京明朝万达科技股份有限公司 \n",
      "北京明朝万达科技股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 4月23日\n",
      "============\n",
      "深圳长润资产管理有限公司-深圳长润创富新三板证券投资基金 投资了 北京明朝万达科技股份有限公司 \n",
      "北京明朝万达科技股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 4月23日\n",
      "============\n",
      "宁波弘基金鼎投资管理中心(有限合伙) 投资了 北京明朝万达科技股份有限公司 \n",
      "北京明朝万达科技股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 4月23日\n",
      "============\n",
      "长江证券股份有限公司 投资了 浙江万达汽车方向机股份有限公司 \n",
      "浙江万达汽车方向机股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 4月23日\n",
      "============\n",
      "浙商汇融投资 投资了 美的集团股份有限公司 \n",
      "美的集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 今年\n",
      "============\n",
      "湖北高投 投资了 美的集团股份有限公司 \n",
      "美的集团股份有限公司 发生了 企业转型 \n",
      " 详细如下：\n",
      "  时间: 今年\"\n",
      "回答：\n",
      "\n",
      "response: \n",
      "根据上下文信息，进行了企业转型的公司有：\n",
      "\n",
      "1. 东易日盛家居装饰集团股份有限公司\n",
      "2. 新希望六和股份有限公司\n",
      "3. 光明乳业股份有限公司\n",
      "4. 大禹节水集团股份有限公司\n",
      "5. 比亚迪股份有限公司\n",
      "6. 深圳市怡亚通供应链股份有限公司\n",
      "7. 北京明朝万达科技股份有限公司\n",
      "8. 浙江万达汽车方向机股份有限公司\n",
      "9. 美的集团股份有限公司"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"总结下哪些公司进行了企业转型？\"\n",
    "\n",
    "graph_rag_pipline(query, exclude_content=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "14530b98-1401-49b2-b4b9-9f3a7213e369",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"瑞幸咖啡\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= and c.name = \"瑞幸咖啡(北京)有限公司\" even_type=\n",
      "\n",
      "        MATCH (i:Investor)-[:INVEST]->(c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1   and c.name = \"瑞幸咖啡(北京)有限公司\" \n",
      "        RETURN i.name as investor,  c.name as company_name, e.name as even_type, r as relation\n",
      "        \n",
      "\n",
      "        MATCH (c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1   and c.name = \"瑞幸咖啡(北京)有限公司\" \n",
      "        RETURN c.name as company_name, e.name as even_type, r as relation\n",
      "        \n",
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"咖啡行业\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"咖啡行业\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"咖啡行业\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= even_type=\n",
      "\n",
      "你是金融知识助手，熟悉各种金融事件，需要根据提供的上下文信息context来回答员工的提问。请直接回答问题，如果上下文信息context没有和问题相关的信息，请直接先回答不知道,要求用中文输出 问题：分析下瑞幸咖啡？ \n",
      "上下文信息：\n",
      "\"瑞幸咖啡(北京)有限公司 发生了 内部丑闻 \n",
      " 详细如下：\n",
      "  时间: 4月2日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 停牌 \n",
      " 详细如下：\n",
      "  时间: 4月7日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 股价异常波动 \n",
      " 详细如下：\n",
      "  数值: ['股价下跌了近89%']\n",
      "  时间: 4月7日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 造假欺诈 \n",
      " 详细如下：\n",
      "  数值: ['22亿元']\n",
      "  时间: 4月2日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 股权质押 \n",
      " 详细如下：\n",
      "  数值: ['4.88亿股', '49.13%', '1.45亿股', '30%']\n",
      "  时间: 本年1月\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 自查违规 \n",
      " 详细如下：\n",
      "  时间: 7月1日晚间\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 复牌 \n",
      " 详细如下：\n",
      "  时间: 5月20日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 资产被查封 \n",
      " 详细如下：\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 董监高—主动离职 \n",
      " 详细如下：\n",
      "  时间: 4月23日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 退市 \n",
      " 详细如下：\n",
      "  时间: 5月19日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 董监高—被迫离职 \n",
      " 详细如下：\n",
      "  时间: 7月2日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 收到起诉 \n",
      " 详细如下：\n",
      "  时间: 4月2日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 其他违规行为 \n",
      " 详细如下：\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 延期披露 \n",
      " 详细如下：\n",
      "  时间: 4月29日\n",
      "============\n",
      "瑞幸咖啡(北京)有限公司 发生了 产品服务负面消息 \n",
      " 详细如下：\"\n",
      "回答：\n",
      "\n",
      "response: \n",
      "瑞幸咖啡在2020年经历了一系列重大事件，对其品牌形象和财务状况造成了严重影响。首先，在4月2日，公司内部曝出了一起涉及22亿元的财务造假丑闻，这导致了股价异常波动，股价下跌近89%，并在同一天收到了起诉。随后，瑞幸咖啡在4月7日被停牌，并于5月19日正式宣布退市。\n",
      "\n",
      "此外，公司在本年1月进行了股权质押，涉及股份达4.88亿股，占总股本的49.13%；其中，1.45亿股的质押比例达到了30%，这可能反映了公司资金链紧张的情况。在7月1日晚间，瑞幸咖啡宣布了自查违规行为，并于7月2日发生了董监高被迫离职事件。\n",
      "\n",
      "值得注意的是，在这一系列负面消息中，瑞幸咖啡的产品服务也受到了负面影响。尽管公司在5月20日短暂复牌，但最终仍未能避免退市的命运。此外，4月23日还出现了董监高主动离职的情况，而4月29日的延期披露行为进一步加剧了市场的担忧。\n",
      "\n",
      "综上所述，瑞幸咖啡在这一系列事件中遭受了严重的信任危机和财务困境，其未来发展充满不确定性。"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"分析下瑞幸咖啡？\"\n",
    "\n",
    "graph_rag_pipline(query, exclude_content=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2180abb-b995-4714-b2da-d64995ec959c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "772a3a7c-1964-4efc-bc79-34f90758b97e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"中瑞深圳\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"中瑞深圳\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"中瑞深圳\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= and i.name = \"中瑞深圳\" company= even_type=\n",
      "\n",
      "        MATCH (i:Investor)-[:INVEST]->(c:Company)-[r:HAPPEN]->(e)\n",
      "        WHERE 1=1  and i.name = \"中瑞深圳\"  \n",
      "        RETURN i.name as investor,  c.name as company_name, e.name as even_type, r as relation\n",
      "        \n",
      "\n",
      "    MATCH (n:Investor)\n",
      "    where n.name CONTAINS \"公司状况\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:Company)\n",
      "    where n.name CONTAINS \"公司状况\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "\n",
      "    MATCH (n:EventType)\n",
      "    where n.name CONTAINS \"公司状况\"\n",
      "    RETURN n.name as name\n",
      "    \n",
      "investor= company= even_type=\n",
      "\n",
      "你是金融知识助手，熟悉各种金融事件，需要根据提供的上下文信息context来回答员工的提问。请直接回答问题，如果上下文信息context没有和问题相关的信息，请直接先回答不知道,要求用中文输出 问题：中瑞深圳有没有投资什么公司？这些公司的状况怎么样 \n",
      "上下文信息：\n",
      "\"中瑞深圳 投资了 科大讯飞股份有限公司 \n",
      "科大讯飞股份有限公司 发生了 裁员 \n",
      " 详细如下：\n",
      "  时间: 2019年\n",
      "============\n",
      "中瑞深圳 投资了 科大讯飞股份有限公司 \n",
      "科大讯飞股份有限公司 发生了 产品创新 \n",
      " 详细如下：\n",
      "  时间: 4月9日\n",
      "============\n",
      "中瑞深圳 投资了 科大讯飞股份有限公司 \n",
      "科大讯飞股份有限公司 发生了 股份解禁 \n",
      " 详细如下：\n",
      "  数值: ['2708.66万股']\n",
      "  时间: 2020年5月20日\"\n",
      "回答：\n",
      "\n",
      "response: \n",
      "中瑞深圳投资了科大讯飞股份有限公司。关于这些公司的状况，有以下信息：科大讯飞在2019年进行了裁员；在4月9日，该公司发生了产品创新；在2020年5月20日，有2708.66万股的股份解禁。具体公司状况需要结合更多市场和财务数据来评估。"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"中瑞深圳有没有投资什么公司？这些公司的状况怎么样\"\n",
    "\n",
    "graph_rag_pipline(query, exclude_content=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "llm"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
